package com.amigo.online.provider.video.mapper;

import java.util.List;

import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Result;
import org.apache.ibatis.annotations.Results;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;
import org.springframework.stereotype.Component;

import com.amigo.online.common.core.mybatis.MyMapper;
import com.amigo.online.common.param.dto.video.ShareVideoDto;
import com.amigo.online.common.param.dto.video.VideoQueryDto;
import com.amigo.online.common.param.result.VideoInfo;
import com.amigo.online.common.param.result.VideoWithWaterMarkInfo;
import com.amigo.online.common.param.result.WaterMarkVideoInfo;
import com.amigo.online.provider.video.model.entity.Video;

import javax.websocket.server.PathParam;

@Mapper
@Component
public interface VideoMapper extends MyMapper<Video> {


    List<Video> queryListWithPage(VideoQueryDto dto);

    List<Video> queryHotRecomendedVideo(@Param("offset") Long offset, @Param("limit") Integer limit);

    List<Video> queryGeneralRecomendedVideo(@Param("offset") Long offset, @Param("limit") Integer limit);

    List<Video> queryByIdList(@Param("videoIdList") List<Long> videoIdList);

    List<Video> getAll();

    List<Video> getVideoListByUserIdList(VideoQueryDto dto);

    int addShare(ShareVideoDto dto);

    void deleteRecommendIfExisted(@Param("id") Long id, @Param("nowSeconds") long nowSeconds);

    int getrRecomendedVideoCount();

    List<Video> getVideoInfoByvideoIds(List<Long> videoIdList);

    List<Long> getRandomRecomendedVideoIds(Object[] videoLookedIds);

    List<Long> getRecomendedVideoId(@Param("minVideoId") Long minVideoId, @Param("maxVideoId") Long maxVideoId, @Param("offset") Integer offset, @Param("limit") Integer limit);

    int updateLook(Long id);

    String getSystemMusicCover(Long musicId);

    @Select("select v.watermark_video as videourl, v.content, v.cover, m.music as musicurl, m.name as musicname, v.user_id as userid from"
            + " video_tbl v, music_tbl m where v.music_id = m.id and v.id = #{id}")
    VideoWithWaterMarkInfo getVideoWithWatermarkById(@Param("id") Long id);

    @Update("update video_tbl set watermark_video = #{waterMarkUrl} where id = #{id}")
    int updateVideoWaterMarkVideoUrl(@Param("id") long id, @Param("waterMarkUrl") String waterMarkUrl);

    @Select({"<script>"
            + " select v.id, v.user_id, v.music_id, v.video, v.cover, v.duration, v.href, v.type, v.ads, v.recommended,"
            + " v.longitude, v.latitude, v.up, v.look, v.comment, v.destroy, v.content, v.title, v.created_time, v.updated_time,"
            + " v.deleted, v.deleted_time, v.width, v.height, v.share, v.location_id, v.watermark_video as waterMarkVideo, v.is_allowe_promote as allowe_promoted, m.name, m.music, m.id as musicId,"
            + " m.type, m.cover as music_cover, m.user_id as m_user_id, m.category,m.recommended as m_recommended, m.video_id,"
            + " m.used, m.created_time as m_created_time from video_tbl v left join music_tbl m on v.music_id = m.id WHERE"
            + " v.deleted = 0  and v.id in"
            + " <foreach collection='ids' item='videoId' open='(' close=')' separator=','>"
            + "	#{videoId}"
            + "	</foreach>"
            + "	ORDER BY field"
            + "	<foreach collection='ids' item='videoId' open='(v.id,' close=')' separator=','>"
            + "	#{videoId}"
            + "	</foreach>"
            + "	</script>"})
    @Results({
            @Result(column = "musicId", property = "music.id"),
            @Result(column = "name", property = "music.name"),
            @Result(column = "type", property = "music.type"),
            @Result(column = "music_cover", property = "music.cover"),
            @Result(column = "m_user_id", property = "music.userId"),
            @Result(column = "category", property = "music.category"),
            @Result(column = "m_recommended", property = "music.recommended"),
            @Result(column = "video_id", property = "music.videoId"),
            @Result(column = "used", property = "music.used"),
            @Result(column = "music", property = "music.music"),
            @Result(column = "m_created_time", property = "music.createdTime")
    })
    List<VideoInfo> listVideoInfoByVideoIds(@Param("ids") List<Long> ids);

    @Select("select share from video_tbl where id = #{id}")
    int getShareNumByVideoId(@Param("id") Long videoId);

    @Select(" select v.id, v.user_id, v.music_id, v.video, v.cover, v.duration, v.href, v.type, v.ads, v.recommended,"
            + " v.longitude, v.latitude, v.up, v.look, v.comment, v.destroy, v.content, v.title, v.created_time, v.updated_time,"
            + " v.deleted, v.deleted_time, v.width, v.height, v.share, v.location_id, v.watermark_video as waterMarkVideo, v.is_allowe_promote as allowe_promoted, m.name, m.music, m.id as musicId,"
            + " m.type, m.cover as music_cover, m.user_id as m_user_id, m.category,m.recommended as m_recommended, m.video_id,"
            + " m.used, m.created_time as m_created_time from video_tbl v left join music_tbl m on v.music_id = m.id WHERE"
            + " v.deleted = 0  and v.id = #{id}")
    @Results({
            @Result(column = "musicId", property = "music.id"),
            @Result(column = "name", property = "music.name"),
            @Result(column = "type", property = "music.type"),
            @Result(column = "music_cover", property = "music.cover"),
            @Result(column = "m_user_id", property = "music.userId"),
            @Result(column = "category", property = "music.category"),
            @Result(column = "m_recommended", property = "music.recommended"),
            @Result(column = "video_id", property = "music.videoId"),
            @Result(column = "used", property = "music.used"),
            @Result(column = "music", property = "music.music"),
            @Result(column = "m_created_time", property = "music.createdTime")
    })
    VideoInfo getVideoInfoByvideoId(@Param("id") Long id);

    @Update("update video_tbl set music_id = #{musicId} where id = #{videoId}")
    int updateVideoMusic(@Param("videoId") Long videoId, @Param("musicId") Long musicId);

    List<Integer> getHomepageVideoCount(Long userId);

    List<Video> getVideoByMusicId(@Param("musicId") Long musicId, @Param("offset") Integer offset, @Param("limit") Integer limit);

    @Select("select count(*) from video_tbl where deleted = 0 and music_id = #{musicId}")
    int getUsedMusicCount(Long musicId);

    @Select("select IFNULL(SUM(up),0) from video_tbl where user_id = #{userId} and deleted = 0")
    int getUpCountByUserId(Long userId);

    @Select("select v.id, v.video, v.title, v.width, v.music_id as musicId, u.id as userId,  u.account from video_tbl v,"
            + " user_tbl u where v.user_id = u.id and  v.id >= #{startId} and v.id <= #{endId} and v.watermark_video is null"
            + " and deleted = 0")
    List<WaterMarkVideoInfo> listVideoByStartIdAndEndId(@Param("startId") Integer startId, @Param("endId") Integer endId);

    @Select("select v.video,v.title,v.width,v.id,v.user_id,v.music_id,u.account from video_tbl v,user_tbl u where (v.watermark_video = NULL OR v.watermark_video = '') and v.deleted = '0' and v.destroy = '0' and u.id = v.user_id")
    List<VideoInfo> getAddWatermaskFailedVideo();

    @Select("select look from video_tbl where id = #{videoId}")
    Integer selectLookNumber(@Param("videoId") Long videoId);

    @Select("select * from video_tbl where watermark_video is null")
    List<Video> selectNoWater();

    @Select("select account from user_tbl where id = #{userId}")
    String selectAcount(@Param("userId") Long userId);

    @Select("select video from video_tbl where deleted = 0")
    List<String> check();
}
